﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

namespace Bohdan.IO
{
    public class CountingStream : Stream
    {
        long _Length = 0;
        long _Position = 0;

        public override bool CanRead { get { return false; } }
        public override bool CanSeek { get { return true; } }
        public override bool CanWrite { get { return true; } }
        public override long Length { get { return _Length; } }
        public override long Position { get { return _Position; } set { _Position = value; } }

        public override void Flush()
        {
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            throw new NotImplementedException();
        }
        public override long Seek(long offset, SeekOrigin origin)
        {
            switch (origin)
            {
                case SeekOrigin.Begin:
                    _Position = offset;
                    break;
                case SeekOrigin.Current:
                    _Position += offset;
                    break;
                case SeekOrigin.End:
                    _Position = _Length + offset;
                    break;
            }
            return _Position;
        }
        public override void SetLength(long value)
        {
            _Length = value;
        }
        public override void Write(byte[] buffer, int offset, int count)
        {
            _Position += count;
            _Length = Math.Max(_Length, _Position);
        }
    }
}
